package com.huang.core.widget.recyclerview.decoration

import android.graphics.Color
import android.view.View
import androidx.annotation.IntRange
import com.huang.core.widget.recyclerview.decoration.base.Y_Divider
import com.huang.core.widget.recyclerview.decoration.base.Y_DividerBuilder
import com.huang.core.widget.recyclerview.decoration.base.Y_ItemDecoration


class GridMarginDecoration(
        private val width: Float,
        private val height: Float,
        @IntRange(from = 1, to = Int.MAX_VALUE.toLong()) private val columnCount: Int
) : Y_ItemDecoration() {

    override fun getDivider(itemPosition: Int, itemCount: Int, view: View): Y_Divider {
        val indexInLine = itemPosition % columnCount

        return if (columnCount == 1) {
            Y_DividerBuilder()
                    .setBottomSideLine(true, Color.TRANSPARENT, height, 0f, 0f)
                    .create()
        } else if (indexInLine == 0) {
            Y_DividerBuilder()
                    .setRightSideLine(true, Color.TRANSPARENT, width / 2, 0f, 0f)
                    .setBottomSideLine(true, Color.TRANSPARENT, height, 0f, 0f)
                    .create()
        } else if (indexInLine == columnCount - 1) {
            Y_DividerBuilder()
                    .setLeftSideLine(true, Color.TRANSPARENT, width / 2, 0f, 0f)
                    .setBottomSideLine(true, Color.TRANSPARENT, height, 0f, 0f)
                    .create()
        } else {
            Y_DividerBuilder()
                    .setLeftSideLine(true, Color.TRANSPARENT, width / 2, 0f, 0f)
                    .setRightSideLine(true, Color.TRANSPARENT, width / 2, 0f, 0f)
                    .setBottomSideLine(true, Color.TRANSPARENT, height, 0f, 0f)
                    .create()
        }
    }
}